package com.wjk.kylin.modules.system.user.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.wjk.kylin.core.common.entity.BaseEntity;
import com.wjk.kylin.core.common.valid.insert;
import com.wjk.kylin.core.common.valid.update;
import com.wjk.kylin.core.constant.CommonConstant;
import lombok.Data;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;

import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import java.util.Date;

/**
 * @author wangjkui
 * @since 2020-06-01
 */
@Data
@ToString
@TableName("sys_user")
public class User extends BaseEntity<User> {

    /** 用户账号 */
    @NotBlank(message = "用户账号不能为空",groups = {insert.class, update.class})
    @Size(min = 0, max = 30, message = "用户账号长度不能超过30个字符",groups = {insert.class, update.class})
    @TableField("user_name")
    private String userName;

    /** 用户昵称 */
    @Size(min = 0, max = 30, message = "用户昵称长度不能超过30个字符",groups = {insert.class, update.class})
    @TableField("nick_name")
    private String nickName;

    /** 用户邮箱 */
    @Email(message = "邮箱格式不正确",groups = {insert.class, update.class})
    @Size(min = 0, max = 50, message = "邮箱长度不能超过50个字符",groups = {insert.class, update.class})
    @TableField("email")
    private String email;

    /** 手机号码 */
    @Size(min = 0, max = 11, message = "手机号码长度不能超过11个字符",groups = {insert.class, update.class})
    @TableField("phone")
    private String phone;

    /** 用户性别 0=男,1=女*/
    @TableField("sex")
    private String sex;

    /** 用户头像 */
    @TableField("avatar")
    private String avatar;

    /** 密码 */
    @TableField(value = "password",updateStrategy = FieldStrategy.NOT_EMPTY)
    @NotBlank(message = "密码不能为空",groups = {insert.class})
    @JsonIgnore
    private String password;

    /** 帐号状态（0正常 1停用） */
    @TableField("status")
    @Pattern(regexp="^[0-1]",message = "状态只能为0或1",groups = {insert.class, update.class})
    private String status;

    /** 最后登陆IP */
    @TableField("login_ip")
    private String loginIp;

    /** 最后登陆时间 */
    @TableField("login_date")
    private Date loginDate;

    /** 部门ID */
    @TableField("dept_id")
    private String deptId;

    /** 是否删除（0 正常 1删除） */
    @TableField("del_flag")
    @TableLogic //todo mybatis plus 逻辑删除注解 . 自带查询会加上这个字段，删除也是设置为1
    private String delFlag = CommonConstant.DelFlagConstant.OK.getCode();

    /** 部门Name */
    @TableField(exist = false)
    private String deptName;

    /** 角色组 */
    @TableField(exist = false)
    private String[] roleIds;

    /** 岗位组 */
    @TableField(exist = false)
    private String[] positionIds;

    //===================================================
    public boolean isAdmin() {
        return isAdmin(getId());
    }
    public static boolean isAdmin(String userId) {
        return StringUtils.isNotBlank(userId) &&"1".equals(userId);
    }
}
